package com.yuanjin.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuanjin.R;
import com.yuanjin.util.ListData_tulingChat;

public class ChatText_Adapter extends BaseAdapter {
	private List<ListData_tulingChat> lists;
	private Context mcontext;
	private RelativeLayout layout;

	public ChatText_Adapter(List<ListData_tulingChat> lists, Context context) {
		this.lists = lists;
		this.mcontext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	// LayoutInflater inflater = LayoutInflater.from(mcontext);
	// // 如果是接受，则加载左会话框模式，然后需要在前面定义一个layout进行接受
	// if (lists.get(position).getFlag() == ListData_tulingChat.RECEIVER) {
	// layout = (RelativeLayout) inflater.inflate(R.layout.chat_leftitem,
	// null);
	// } else if (lists.get(position).getFlag() == ListData_tulingChat.SEND) {
	// layout = (RelativeLayout) inflater.inflate(R.layout.chat_rightitem,
	// null);
	// }
	// // 显示聊天内容
	// TextView tv = (TextView) layout.findViewById(R.id.tv);
	// TextView time = (TextView) layout.findViewById(R.id.time);
	// tv.setText(lists.get(position).getContent());
	// // 若接收到的时间数据不为""(空字符串),则显示时间。否则隐藏显示时间的textview
	// if (lists.get(position).getTime() != "") {
	// time.setVisibility(View.VISIBLE);
	// time.setText(lists.get(position).getTime());
	// } else {
	// time.setVisibility(View.GONE);
	// }
	//
	// return layout;
	// }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		LayoutInflater inflater = LayoutInflater.from(mcontext);
		if (convertView == null) {
			// 如果是接收，则加载左会话框模式，然后需要在前面定义一个layout进行接收
			if (lists.get(position).getFlag() == ListData_tulingChat.RECEIVER) {
				convertView = (RelativeLayout) inflater.inflate(
						R.layout.chat_leftitem, null);
			} else if (lists.get(position).getFlag() == ListData_tulingChat.SEND) {
				convertView = (RelativeLayout) inflater.inflate(
						R.layout.chat_rightitem, null);
			}
			vh = new ViewHolder();
			vh.tv = (TextView) convertView.findViewById(R.id.tv);
			vh.time = (TextView) convertView.findViewById(R.id.time);
			//converview与vh建立对应关系
			convertView.setTag(vh);
		}else {
			vh = (ViewHolder) convertView.getTag();
		}
		vh.tv.setText(lists.get(position).getContent());
		// 若接收到的时间数据不为""(空字符串),则显示时间。否则隐藏显示时间的textview
		if (lists.get(position).getTime() != "") {
			vh.time.setVisibility(View.VISIBLE);
			vh.time.setText(lists.get(position).getTime());
		} else {
			vh.time.setVisibility(View.GONE);
		}

		return convertView;
	}

	class ViewHolder {
		TextView tv;
		TextView time;
	}
}
